﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class NamePropertyDescriptor
        Inherits DynamicPropertyDescriptor
        ' Methods
        Friend Sub New(ByVal serviceProvider As IServiceProvider, ByVal actualPropDesc As PropertyDescriptor)
            MyBase.New(serviceProvider, actualPropDesc)
        End Sub

        Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
            Return False
        End Function

        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            Dim activity As Activity = [TryCast](component,Activity)
            If (Not activity Is Nothing) Then
                Dim service As IIdentifierCreationService = [TryCast](activity.Site.GetService(GetType(IIdentifierCreationService)),IIdentifierCreationService)
                If (service Is Nothing) Then
                    Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IIdentifierCreationService).FullName }))
                End If
                Dim identifier As String = [TryCast](value,String)
                service.ValidateIdentifier(activity, identifier)
                Dim ignoreCase As Boolean = (CompilerHelpers.GetSupportedLanguage(activity.Site) = SupportedLanguages.VB)
                Dim dataSourceClass As Type = Helpers.GetDataSourceClass(Helpers.GetRootActivity(activity), activity.Site)
                If ((Not dataSourceClass Is Nothing) AndAlso (Not ActivityBindPropertyDescriptor.FindMatchingMember(identifier, dataSourceClass, ignoreCase) Is Nothing)) Then
                    Throw New ArgumentException(SR.GetString("Error_ActivityNameExist", New Object() { identifier }))
                End If
                Dim service2 As IMemberCreationService = [TryCast](activity.Site.GetService(GetType(IMemberCreationService)),IMemberCreationService)
                If (service2 Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(IMemberCreationService).FullName }))
                End If
                Dim host As IDesignerHost = [TryCast](activity.Site.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (host Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(IDesignerHost).FullName }))
                End If
                Dim newClassName As String = identifier
                Dim num As Integer = host.RootComponentClassName.LastIndexOf("."c)
                If (num > 0) Then
                    newClassName = (host.RootComponentClassName.Substring(0, (num + 1)) & identifier)
                End If
                service2.UpdateTypeName([TryCast](DirectCast(host.RootComponent, Activity).GetValue(WorkflowMarkupSerializer.XClassProperty),String), newClassName)
                DirectCast(host.RootComponent, Activity).SetValue(WorkflowMarkupSerializer.XClassProperty, newClassName)
                MyBase.SetValue(component, value)
                DesignerHelpers.UpdateSiteName(DirectCast(host.RootComponent, Activity), identifier)
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property Category As String
            Get
                Return SR.GetString("Activity")
            End Get
        End Property

        Public Overrides ReadOnly Property Description As String
            Get
                Return SR.GetString("RootActivityNameDesc")
            End Get
        End Property

    End Class
End Namespace

